package com.yanqu.road.dao.impl.cross.cricket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.cricket.court.CricketCourtDefenseData;
import com.yanqu.road.entity.cricket.court.CricketCourtUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossCricketCourtUserDaoImpl extends TempDataObjectDao<CricketCourtUserData> {

    public Map<Long, Map<Long, CricketCourtUserData>> getCricketCourtUserDataMap() {
        Map<Long, Map<Long, CricketCourtUserData>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String selectSql = "select * from t_c_cricket_court_user_data";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CricketCourtUserData data = parse(rs);
                Map<Long, CricketCourtUserData> userMap = map.getOrDefault(data.getServerId(), new ConcurrentHashMap<>());
                userMap.put(data.getUserId(), data);
                map.put(data.getServerId(), userMap);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String insertSql = "insert into `t_c_cricket_court_user_data`(serverId,userId,courtId,groupId,score,param,round,lastCourtId,lastRank,lastGroupNum,defenseDataList,courtBadgeId,updateTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CricketCourtUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<>();
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getCourtId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, data.getParam()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getLastCourtId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getLastRank()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getLastGroupNum()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDefenseDataList())));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getCourtBadgeId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        if (getDb().executeNoneQuery(insertSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    private static String updateSql = "update `t_c_cricket_court_user_data` set `courtId`=?,`groupId`=?,`score`=?,`param`=?,`round`=?,`lastCourtId`=?,`lastRank`=?,`lastGroupNum`=?,`defenseDataList`=?,`courtBadgeId`=?,`updateTime`=? where serverId=? and userId=?";

    @Override
    protected boolean updateData(CricketCourtUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<>();
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getCourtId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, data.getParam()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getLastCourtId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getLastRank()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getLastGroupNum()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDefenseDataList())));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getCourtBadgeId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CricketCourtUserData parseData(ResultSet rs) throws SQLException {
        CricketCourtUserData data = new CricketCourtUserData();
        data.setServerId(rs.getLong("serverId"));
        data.setUserId(rs.getLong("userId"));
        data.setCourtId(rs.getInt("courtId"));
        data.setGroupId(rs.getInt("groupId"));
        data.setScore(rs.getLong("score"));
        data.setParam(rs.getString("param"));
        data.setRound(rs.getInt("round"));
        data.setLastCourtId(rs.getInt("lastCourtId"));
        data.setLastRank(rs.getInt("lastRank"));
        data.setLastGroupNum(rs.getInt("lastGroupNum"));
        data.setUpdateTime(rs.getLong("updateTime"));
        String defenseTeamList = rs.getString("defenseDataList");
        if (!StringUtils.isNullOrEmpty(defenseTeamList)) {
            data.setDefenseDataList(JSON.parseObject(defenseTeamList, new TypeReference<List<CricketCourtDefenseData>>() {
            }));
        }
        data.setCourtBadgeId(rs.getInt("courtBadgeId"));
        return data;
    }
}
